﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Projektarbete
{
    static class AIManager
    {
        private static LinkedList<EnemyBase> enemyList;
        private static LinkedList<MerchantBase> merchantList;
        private static Random random;

        /// <summary>
        /// To be called in the beginning of the application
        /// </summary>
        public static void SetUp()
        {
            enemyList = new LinkedList<EnemyBase>();
            merchantList = new LinkedList<MerchantBase>();

            random = Global.random;
        }


        /// <summary>
        /// To be called every frame
        /// </summary>
        public static void Update(Vector2 playerPosition)
        {
            foreach (EnemyBase enemy in enemyList)
            {
                enemy.Update(playerPosition);
            }
        }

        public static bool HitTestPlayer(EnemyBase enemy)
        {
            if (Math.Abs(Player.position.X - enemy.getPosition.X) < 24 && Math.Abs(Player.position.Y - enemy.getPosition.Y) < 24)
                return true;

            return false;
        }

        public static bool HitTestPlayer(MerchantBase merchant)
        {
            if (Math.Abs(Player.position.X - merchant.getPosition.X) < 24 && Math.Abs(Player.position.Y - merchant.getPosition.Y) < 24)
                return true;

            return false;
        }

        /// <summary>
        /// Interacts with the merchant at the given position
        /// </summary>
        /// <param name="facingPosition">position of the merchant</param>
        /// <returns></returns>
        public static bool InteractWithMerchant(Vector2 facingPosition)
        {
            //TODO: ADD TILEDPOSITION VERSION
            Vector2 facingPositionToWorld = new Vector2(facingPosition.X * 24, facingPosition.Y * 24);
            foreach (MerchantBase merchant in merchantList)
            {
                if (merchant.getPosition == facingPositionToWorld)
                {
                    PopupManager.OpenMerchant();
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Removes the specified item from the merchant's inventory
        /// </summary>
        /// <param name="item">item to remove</param>
        /// <param name="position">position of the merchant</param>
        public static void RemoveFromMerchantInventory(Item item, Vector2 position)
        {
            foreach (MerchantBase merchant in merchantList)
            {
                if (merchant.getPosition == position)
                {
                    merchant.inventory.RemoveItem(item);
                }
            }
        }

        /// <summary>
        /// Adds the specified item to the merchant's inventory
        /// </summary>
        /// <param name="item">item to add</param>
        /// <param name="position">position of the merchant</param>
        public static void AddToMerchantInventory(Item item, Vector2 position)
        {
            foreach (MerchantBase merchant in merchantList)
            {
                if (merchant.getPosition == position)
                {
                    merchant.inventory.AddItem(item);
                }
            }
        }

        public static bool isAIAt(Vector2 position)
        {
            foreach (MerchantBase merchant in merchantList)
                if (merchant.getPosition == position)
                    return true;

            foreach (EnemyBase enemy in enemyList)
                if (enemy.getPosition == position)
                    return true;

            return false;
        }

        public static MerchantBase getMerchant(Vector2 position)
        {
            foreach (MerchantBase merchant in merchantList)
            {
                if (merchant.getPosition == position)
                {
                    return merchant;
                }
            }

            throw new Exception("Borde inte hända. Ropa på David");
        }

        public static void CombatTime()
        {
            throw new Exception("COMBAT TIEEEEEM");
        }

        public static void AddEnemy(EnemyBase enemy)
        {
            enemyList.AddLast(enemy);
        }

        public static void AddMerchant(MerchantBase merchant)
        {
            merchantList.AddLast(merchant);
        }

        public static void KillEnemy(EnemyBase enemy)
        {
            enemyList.Remove(enemy);
        }

        public static void KillMerchant(MerchantBase merchant)
        {
            merchantList.Remove(merchant);
        }

        /// <summary>
        /// Draws all AI units. To be called every time draw is called in main
        /// </summary>
        public static void Draw(SpriteBatch spriteBatch)
        {
            foreach (EnemyBase enemy in enemyList)
            {
                //TODO: make better
                if (Global.CanPlayerSee(Global.ToTilePosition(enemy.getPosition)))
                    enemy.Draw(spriteBatch);
            }

            foreach (MerchantBase merchant in merchantList)
            {
                if (Global.CanPlayerSee(Global.ToTilePosition(merchant.getPosition)))
                    merchant.Draw(spriteBatch);
            }
        }
    }
}
